package com.zkingsoft.actions.admin;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.anotations.RemoveRequestToken;
import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.model.SysDataDictionary;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.SysDataDictionaryService;

/**
 * @description 数据字典公用action
 * @author 罗袁宏
 * @email 547615133@qq.com
 * @date 2016-07-12
 */
@Controller
@RequestMapping(value = "admin/dataDictionary")
public class DateDictionaryController extends BaseController {
	// 数据字典
	@Resource
	private SysDataDictionaryService currentService;

	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(HttpServletRequest request, SysDataDictionary dictionary,
			PaginationVO pageVo) {
		return showList(currentService, dictionary, pageVo);
	}

	@RequestMapping(value = "/addOrModify")
	@RemoveRequestToken
	public @ResponseBody AjaxResult addOrModify(SysDataDictionary dictionary) {
		if (dictionary.getId() != null) {
			return modify(currentService, dictionary, "数据字典");
		} else {
			return add(currentService, dictionary, "数据字典");
		}
	}

	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		return remove(currentService, keys);
	}

	@RequestMapping(value = "/editForm")
	@SaveRequestToken
	public String editForm(Long id, HttpServletRequest request) {
		SysDataDictionary dictionary;
		List<String> list = currentService.getDictionaryType();
		request.setAttribute("dictionarys", list);
		if (id != null) {
			dictionary = currentService.findById(id);
			request.setAttribute("obj", dictionary);
		}
		return "admin/dictionary-form";
	}

	/**
	 * =======================================================
	 * *******************公共的数据访问方法********************
	 * =======================================================
	 */

	@RequestMapping(value = "/all")
	public @ResponseBody AjaxResult all() {
		return findByModel(currentService, null);
	}

	/**
	 * 显示所有的会员信息前查询
	 */
	@RequestMapping(value = "/showDataDictionary")
	public @ResponseBody AjaxResult find(SysDataDictionary dataDictionary) {
		List<SysDataDictionary> dataDictionarys = currentService.findByModel(dataDictionary);
		return new AjaxResult(AjaxResult.STATUS_OK, null, null, dataDictionarys, 0);
	}

	/**
	 * 查询数据字段类型
	 * 
	 * @author 罗凯
	 * @return
	 */
	@RequestMapping(value = "/getDictionaryType")
	public @ResponseBody AjaxResult getDictionaryType() {
		List<String> dics = currentService.getDictionaryType();
		List<SysDataDictionary> dictionarys = new ArrayList<SysDataDictionary>();
		SysDataDictionary dataDictionary;
		for (String dic : dics) {
			dataDictionary = new SysDataDictionary();
			dataDictionary.setType(dic);
			dictionarys.add(dataDictionary);
		}
		return new AjaxResult(AjaxResult.STATUS_OK, null, null, dictionarys, 0);
	}
 
}